package com.wyw.function.learning.demo9;

import com.wyw.function.learning.common.Shop;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Title   使用异步API
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
public class DemoClient22 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        test1();
    }

    public static void test1() throws ExecutionException, InterruptedException {
        Shop shop = new Shop("BestShop");
        long start = System.currentTimeMillis();
//        Future<Double> future = shop.getPriceAsync("Are you OK?");
        Future<Double> future = shop.getPriceAsync2("Are you OK?");
        long endFirst = System.currentTimeMillis();
        System.out.println("invoke " + (endFirst - start) + " md");
        // 其他操作
        doSomeThingElse();
        Double price = future.get();
        System.out.println("price "+ price);
        System.out.println("future time "+ (System.currentTimeMillis() - endFirst) + " ms");

    }

    private static void doSomeThingElse() {
        for(int i = 0; i< 10; i++) {
            System.out.print(i);
        }
        System.out.println();
    }

}
